package com.example.xViewChat.NettyClient;

import android.content.Context;
import android.content.Intent;
import com.example.xViewChat.Bean.IntentBean;
import com.example.xViewChat.Service.PushService;
import com.example.xViewChat.UI.LoginAcitivity;
import com.example.xViewChat.UI.WelocomeActivity;
import io.ganguo.chat.core.transport.IMResponse;

/**
 * Created by hasee on 2016-03-31.
 */
public class PushManager {
    public static String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";

    public static String ACTION_CONNECTION = "ACTION_CONNECTION";

    public static String ACTION_CONNECTION_STATUS = "ACTION_CONNECTION_STATUS";

    public static String ACTION_SENDREQUEST = "ACTION_SENDREQUEST";

    public static String ACTION_DISCONNECTION = "ACTION_DISSENDREQUEST";

    public static String ACTION_DESTORY = "ACTION_DESTORY";

    public static String SERVICE_ACTION = "SERVICE_ACTION";

    public static String KEY_SEND_BODY = "KEY_SEND_BODY";

    static String KEY_CONNECTION_STATUS = "KEY_CONNECTION_STATUS";

    static ConnectorManager manager;

    static Context context;

    /**
     * ��ʼ��,���ӷ���ˣ��ڳ�������ҳ���� ��Application�����
     *
     * @param context
     * @param ip
     * @param port
     */
    public static void init(Context context, String ip, int port) {

        PushManager.context = context;
        CacheTools.putBoolean(context, CacheTools.KEY_DESTORYED, false);
        CacheTools.putBoolean(context, CacheTools.KEY_MANUAL_STOP, false);
        CacheTools.putBoolean(context, CacheTools.KEY_AUTO_RECONNECT, true);//是否自动重连
        CacheTools.putString(context, CacheTools.KEY_SERVIER_HOST, ip);
        CacheTools.putInt(context, CacheTools.KEY_SERVIER_PORT, port);

        Intent serviceIntent = new Intent(context, PushService.class);
        serviceIntent.putExtra(CacheTools.KEY_SERVIER_HOST, ip);
        serviceIntent.putExtra(CacheTools.KEY_SERVIER_PORT, port);
        serviceIntent.putExtra(SERVICE_ACTION, ACTION_CONNECTION);
        context.startService(serviceIntent);
    }

    protected static void init(Context context) {

        boolean isManualStop = CacheTools.getBoolean(context, CacheTools.KEY_MANUAL_STOP);
        boolean isManualDestory = CacheTools.getBoolean(context, CacheTools.KEY_DESTORYED);

        if (isManualStop || isManualDestory) {
            return;
        }

        String host = CacheTools.getString(context, CacheTools.KEY_SERVIER_HOST);
        int port = CacheTools.getInt(context, CacheTools.KEY_SERVIER_PORT);

        init(context, host, port);

    }

    public static void login(){

    }
    public static void destory(Context context) {


        CacheTools.putBoolean(context, CacheTools.KEY_DESTORYED, true);
        CacheTools.putString(context, CacheTools.KEY_ACCOUNT, null);

        Intent serviceIntent = new Intent(context, PushService.class);
        serviceIntent.putExtra(SERVICE_ACTION, ACTION_DESTORY);
        context.startService(serviceIntent);


    }


    public static void send(Context context, IntentBean intentBean) {
        Intent serviceIntent = new Intent(context, PushService.class);
        serviceIntent.putExtra(SERVICE_ACTION, ACTION_SENDREQUEST);
        serviceIntent.putExtra(KEY_SEND_BODY, intentBean);
        context.startService(serviceIntent);
    }
}
